<template>
<x-main>
  <x-input :required="true" label="姓名" v-model="state.form.name" placeholder="请输入员工姓名"></x-input>
  <x-input :disabled="!$empty(state.form.userId)" :required="true" label="登录账号" v-model="state.form.username" placeholder="请输入员工账号"></x-input>
  <x-input v-if="$empty(state.form.userId)" :required="true" label="登录密码" type="password" v-model="state.form.password" placeholder="请输入员工密码"></x-input>
  <x-select :required="true" label="账号类型" v-model="state.form.roleCode" :options="state.accountTypeOptions"></x-select>
  <x-choose :required="true" label="所属门店" v-model="state.form.storeIds" type="remote" options="STORE"></x-choose>
  <x-cell :border="false" title="状态">
    <template #value>
      <x-radio-button align="right" v-model="state.form.status" :list="OPEN_STATUS"></x-radio-button>
    </template>
  </x-cell>
</x-main>
  <x-bottom>
    <view class="x-btn x-btn-primary" @click="onSubmit">提交</view>
  </x-bottom>
</template>

<script setup>
import XRadioButton from '@/components/form/radioButton'
import XChoose from '@/components/form/choose'
import userStore from '@/stores/framework/user'
const UserStore = userStore();

import staff from '@/stores/modules/staff';
const Store  = staff();
import {Helper, Http, Msg, Constant} from 'core';
import {reactive} from "vue";
import {onLoad} from "@dcloudio/uni-app";

const state = reactive({
  form :{
    userId: "",
    name: "",
    username: "",
    password: "",
    status: "1",
    roleCode: Constant.ROLE_STAFF,
    storeIds: []
  },
  accountTypeOptions: []
})

onLoad(async (option) => {
  state.form.userId = option.id;
  state.accountTypeOptions = [{label: "普通员工", value: Constant.ROLE_STAFF}];

  if(UserStore.hasRole(Constant.ROLE_COMPANY_ADMIN)){
    state.accountTypeOptions.push({label: "门店管理员", value: Constant.ROLE_STORE_ADMIN})
  }
  if(! Helper.empty(option.id)) {
    state.form = await Store.getInfo(option.id);
  }
})

const onSubmit = async () => {
  let isOk = Helper.empty(state.form.userId) ? await Store.save(state.form) : await Store.update(state.form);
  if(isOk) {
    uni.navigateBack();
  }
}

</script>

<style scoped lang="less">

</style>
